package com.wjc.server.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.server.dto.RoleResourceDto;
import com.wjc.server.dto.PageDto;
import com.wjc.server.entity.RoleResource;
import com.wjc.server.entity.RoleResourceExample;
import com.wjc.server.mapper.RoleResourceMapper;
import com.wjc.server.util.CopyUtil;
import com.wjc.server.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class RoleResourceService {
    @Resource
    private RoleResourceMapper roleResourceMapper;

    @Resource
    private SnowFlake snowFlake;

    //查询
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        RoleResourceExample roleResourceExample = new RoleResourceExample();
        List<RoleResource> roleResourceList= roleResourceMapper.selectByExample(roleResourceExample);
        PageInfo<RoleResource> pageInfo=new PageInfo<>(roleResourceList);
        pageDto.setTotal(pageInfo.getTotal());

        List<RoleResourceDto> roleResourceDtosList = CopyUtil.copyList(roleResourceList,RoleResourceDto.class);
        pageDto.setList(roleResourceDtosList);

    }

    //保存
    public void save(RoleResourceDto roleResourceDto){

        RoleResource roleResource=CopyUtil.copy(roleResourceDto,RoleResource.class);
        if (ObjectUtils.isEmpty(roleResource.getId())){
            //新增
            roleResource.setId(snowFlake.nextId());
            roleResourceMapper.insert(roleResource);
        }else {
            roleResourceMapper.updateByPrimaryKey(roleResource);
        }

    }

    public void delete(Long id){
        roleResourceMapper.deleteByPrimaryKey(id);
    }
}
